/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.gui.scatterplot;

import java.util.Collection;
import java.util.Vector;

import controlP5.ControlP5;
import controlP5.ControllerGroup;
import de.fuberlin.processing.who.cancer.data.structs.Struct_09_HealthFinancingToAirPollution;
import de.fuberlin.processing.who.cancer.interaction.LocationBroadcaster;
import de.fuberlin.processing.who.cancer.interaction.LocationModifier;

import processing.core.PApplet;


public class Cell_09_HealthFinancingToAirPollution extends CustomCell{


	public Cell_09_HealthFinancingToAirPollution( ControlP5 theControlP5,
			ControllerGroup theParent, String theName,
			CellConstructionAndProcessingParcel cellConstructionAndProcessingParcel,
			Collection coll, float theX, float theY, int theWidth, int theHeight) {
		super( theControlP5, theParent, theName, cellConstructionAndProcessingParcel,
				coll, theX, theY, theWidth, theHeight);
		this.locationModifier = LocationBroadcaster.get();
	}


	private LocationModifier locationModifier;
	

	@Override
	public void drawCell(PApplet p) {
	
		Vector<Struct_09_HealthFinancingToAirPollution> data = (Vector<Struct_09_HealthFinancingToAirPollution>) coll;

		float maxHealthFinancing = 0;
		int maxAirPollution = 0;

		float minHealthFinancing = Float.MAX_VALUE;
		int minAirPollution = Integer.MAX_VALUE;

		for (Struct_09_HealthFinancingToAirPollution st : data) {
			if (st.healthFinancing > maxHealthFinancing)
				maxHealthFinancing = st.healthFinancing;
			if (st.healthFinancing < minHealthFinancing)
				minHealthFinancing = st.healthFinancing;
			if (st.airPollution > maxAirPollution)
				maxAirPollution = st.airPollution;
			if (st.airPollution < minAirPollution)
				minAirPollution = st.airPollution;
		}

		float m1 = 0;
		float m2 = 0;

		Collection<Integer> selectedLocations = locationModifier.getLocations();
		
		p.pushStyle();
		for (Struct_09_HealthFinancingToAirPollution st : data) {
			m1 = p.map(st.healthFinancing, minHealthFinancing,
					maxHealthFinancing, margin, (int) width - margin);
			m2 = p.map(st.airPollution, minAirPollution, maxAirPollution,
					margin, (int) height - margin);
			p.stroke( selectedLocations.contains( st.locationId) ? COLOR_SELECTED : COLOR_NORMAL);
			p.point( left + m1, top + height - m2);
		}
		p.popStyle();
		
	}

}
